//
//  HOHopeHandlerViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/15/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOHopeHandlerViewController.h"
#import "HOConstants.h"
#import "HOHopeHanderEntity.h"
#import "HOStaticDataMgr.h"


@interface HOHopeHandlerViewController ()
{
	NSMutableDictionary *handlerDic;
}
@end

@implementation HOHopeHandlerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self initTableView];
	[self requestDataWithMax:0];
	handlerDic = [[NSMutableDictionary alloc] init];
}
- (void)requestDataWithMax:(int)maxId
{
//	if([self.dataList count] == 0)
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetRepairmanList, kHO_FIELD_DepartmentId:[NSNumber numberWithInt:self.departmentEntity.departmentId]}];
}

- (void)viewWillAppear:(BOOL)animated
{
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"选择希望受理人", @"选择希望受理人")];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return CELL_HEIGHT;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger count = [self.tableViewDataList count];
	[self updateNoResultView:count];;
	return count;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
	HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
	}
	HOHopeHanderEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.row];
	cell.textLabel.text = entity.userName;
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	HOHopeHanderEntity *entity = [self.tableViewDataList objectAtIndex:indexPath.row];
	[self.delegate didSelectHopeHandler:entity];
	[self goBack];
	
}

#pragma mark
- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetRepairmanList])
	{
		if(![response isValidatedArray:response.dataNode])
		{
			return;
		}
		NSArray *array = response.dataNode;
//		[[HOStaticDataMgr sharedInstance] didReceiveHopeHandlerList:response.dataNode];
		for(NSInteger i = 0; i < [array count];i ++)
		{
			NSDictionary *dic = [array objectAtIndex:i];
			HOHopeHanderEntity *entity = [self getHopeHandlerEntityWithId:[[dic objectForKey:kHO_FIELD_UserId] intValue]];
			[entity parseDic:dic];
			[self.tableViewDataList addObject:entity];
		}
		[baseTableview reloadData];
	}
}

- (HOHopeHanderEntity *)getHopeHandlerEntityWithId:(int)Id
{
	HOHopeHanderEntity *entity = [handlerDic objectForKey:[NSNumber numberWithInt:Id]];
	if(entity == nil)
	{
		entity = [[[HOHopeHanderEntity alloc] init] autorelease];
		[handlerDic setObject:entity forKey:[NSNumber numberWithInt:Id]];
		entity.userId = Id;
		
	}
	return entity;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
